package Prova::Controller::TestCase;

use strict;
use warnings;
use base 'Catalyst::Controller';
use File::Path;
use File::Temp qw(tempdir);
use Prova::FixedForms;
use Data::Dumper;

=head1 NAME

Prova::Controller::TestCase - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

=head2 default

default action

=cut

sub default : Private {
    my ( $self, $c ) = @_;

    $c->forward('list');
}

=head2 list

list action

=cut

sub list : Private {
    my ( $self, $c ) = @_;

    $c->stash->{template} = 'testcase/list.tt';

    my $where = { is_recording => { '!=', 1 } };

    if ( $c->session->{'testcase_filter'} == Constants->TESTCASE_FILTER_TYPE_INDIVIDUAL ) {
        if ( $c->session->{'account_id'} ) {
            $where->{'account_id'} = $c->session->{'account_id'} || 0;
        }
    }

    my $case = [ $c->model('DBIC::TestCase')->search( $where, { order_by => 'order_by' } ) ];
    $c->stash->{testcase} = $case;

    unless ( $c->session->{all_tags} ) {
        $c->forward('/tagmgr/get_all_tags');
    }
}

=head2 listsort

sort action at list

=cut

sub listsort : Local {
    my ( $self, $c, $checksum ) = @_;
    my $params = $c->req->params;
    if ( $params->{swap} ) {
        my $testcase1 = $c->model('DBIC::TestCase')->search( checksum => $checksum )->first;
        my $testcase2 = $c->model('DBIC::TestCase')->search( checksum => $params->{swap} )->first;
        if ( $testcase1 && $testcase2 ) {
            my ( $order1, $order2 ) = ( $testcase2->order_by, $testcase1->order_by );
            $testcase1->order_by($order1);
            $testcase2->order_by($order2);
            $testcase1->update;
            $testcase2->update;
        }
    }
    $c->res->redirect('/testcase');
}

=head2 filter

switch indivisual and all test cases

=cut

sub filter : Local {
    my ( $self, $c ) = @_;
    if ( $c->request->method eq 'POST' && $c->req->params->{testcase_filter} ) {
        $c->session->{'testcase_filter'} = $c->req->params->{testcase_filter};
    }
    $c->res->redirect('/testcase');
}

=head2 edit

edit action

=cut

sub edit : Local {
    my ( $self, $c, $checksum ) = @_;

    $c->log->debug( "===== checksum : " . $checksum );
    $c->stash->{template} = 'testcase/edit.tt';

    my $testcase;
    if ($c->stash->{testcase}) {
        $testcase = $c->stash->{testcase};
    } elsif ($checksum) {
        $testcase = $c->model('DBIC::TestCase')->search( checksum => $checksum  )->first;
    } else {
        $c->detach('/pagenotfound/publish');
    }

    $c->stash->{testcase}   = $testcase;
    $c->stash->{datablock}  = $c->forward( '/datablock/find_by_id', [ $testcase->id ] );
    $c->stash->{tags}       = $c->forward( '/tagmgr/get_tags', [ $testcase->id ] );
    $c->stash->{all_tags}   = $c->forward('/tags/get_tags_name');
    $c->stash->{fixedforms} = [ Prova::FixedForms::types($c) ];

    my $method = $c->request->method;
    $c->log->debug( '===== method : ' . $method );
    if ( $method eq 'POST' ) {
        $c->log->debug('=====');

        # CHECK ERRORS

        my $params = $c->req->params;

        $testcase->account_id( $c->session->{'account_id'} || 0 );
        $testcase->name( $params->{'name'} );
        $testcase->description( $params->{'description'} );
        $testcase->is_recording(0);
        $testcase->order_by( $self->max_order_by($c) + 1 )
          unless $params->{order_by};
        $testcase->update;

        # tags
        my $req_tags = $c->req->param('tags');
        $req_tags =~ s/\s*,\s*/,/g;
        my @tags = split ',', $req_tags;
        $c->forward( '/testcasestags/remove_tags', [ $testcase->id ] );
        $c->forward( '/tagmgr/create', [ $testcase->id, \@tags ] ) if @tags;
        $c->stash->{tags} = \@tags if @tags;
        $c->stash->{status}   = 1;
        $c->stash->{testcase} = $testcase;
        my $tmp_tags = $c->forward( '/tagmgr/get_tags', [ $testcase->id ] );
        $c->session->{all_tags}->{ $testcase->id } = $tmp_tags;

        # application depending function
        if ( $c->req->param('fixedform') ) {
            my $class = 'Prova::FixedForms::' . $c->req->param('fixedform');
            eval "use $class";
            $class->execute($c, $testcase);
        }

        if ( $c->req->param('return_path') ) {
            $c->res->redirect( '/' . $c->req->param('return_path') );
        } else {
            $c->stash->{message_title}   = 'Success!';
            $c->stash->{message_content} = 'TestCase have been updated.';
        }
    }
    else {

        # accessors in same testcase
        $c->forward( '/accessor/get_testcase_accessors', [ $testcase->id ] ) if $testcase;
    }
}

=head2 max_order_by

get max order_by value in test_case

=cut

sub max_order_by {
    my ( $self, $c ) = @_;

    my ($testcase) = $c->model('DBIC::TestCase')->search(
        {},
        {
            order_by => 'order_by desc',
            limit    => 1
        }
    )->first;
    return $testcase ? $testcase->order_by : 0;
}

=head2 delete

delete action

=cut

sub delete : Local {
    my ( $self, $c, $checksum ) = @_;

    $c->log->debug( "=== " . $checksum );
    my $testcase = $c->model('DBIC::TestCase')->search( checksum => $checksum )->first;
    $testcase->delete($c);

    $c->res->redirect('/testcase');
}

=head2 recording

recording action

=cut

sub recording : Local {
    my ( $self, $c ) = @_;

    $c->stash->{testcase} = $c->forward('is_recording');
    $c->forward('edit');
}


=head2 is_recording

get recording test_case model

=cut

sub is_recording : Private {
    my ( $self, $c ) = @_;

    $c->log->debug('recording start');

    $c->forward('/account/get_model');

    my ($testcase2) = $c->model('DBIC::TestCase')->search( { is_recording => 2, account_id => $c->stash->{account}->id } );
    if ( defined $testcase2 ) {
        return $testcase2;
    } else {
        my ($testcase1) = $c->model('DBIC::TestCase')->search( is_recording => 1 );
        if (defined $testcase1) {
            return $testcase1;
        } else {
            return undef;
        }
    }
}

=head2 start_recording

=cut

sub start_recording : Private {
    my ( $self, $c ) = @_;

    my %data = (
        checksum     => Prova::Checksum->calc,
        name         => '',
        description  => '',
        is_recording => 1,
        order_by     => 0
    );
    my ($testcase) = $c->model('DBIC::TestCase')->create( \%data );
    $c->log->debug("===== create testcase");
    return $testcase;
}

=head2 control

start and stop control action

=cut

sub control : Local {
    my ( $self, $c ) = @_;

    my @testcases = $c->model('DBIC::TestCase')->search( { account_id => $c->session->{'account_id'} } );
    foreach ( @testcases ) {
        if ( $_->checksum eq $c->req->args->[0] ) {
            $_->is_recording( ($_->is_recording == 2) ? 0 : 2 );
            $_->update();
        } else {
            $_->is_recording(0);
            $_->update();
        }
    }
    $c->res->redirect('/testcase');
}

=head1 AUTHOR

Shanon Inc, E<lt>http://www.shanon.co.jp/E<gt>

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;

